package gogogo.liwushuo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import gogogo.liwushuo.R;
import gogogo.liwushuo.bean.AuthorBean;
import gogogo.liwushuo.bean.TitleBean;
import gogogo.liwushuo.listener.OnItemClickListener;

/**
 * Created by chenjun on 2016/7/21.
 */
public class RecyclerAuthorAdapter extends RecyclerView.Adapter<RecyclerAuthorAdapter.Holder> {

    private Context mContext;
    private String tag;
    private List<AuthorBean.DataBean.PostsBean> list;
    private List<TitleBean.DataBean.PostsBean> list1;

    public RecyclerAuthorAdapter(Context context, List<AuthorBean.DataBean.PostsBean> list) {
        this.mContext = context;
        this.list = list;
    }

    public RecyclerAuthorAdapter(Context context, List<TitleBean.DataBean.PostsBean> list, String tag) {
        this.mContext = context;
        this.list1 = list;
        this.tag = tag;
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_author, parent, false);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(final Holder holder, final int position) {
        SimpleDateFormat format = new SimpleDateFormat("MM-dd");
        AuthorBean.DataBean.PostsBean postsBean = list.get(position);
        String s = format.format(new Date(postsBean.getCreated_at() * 1000));
        holder.tvTime.setText(s);
        holder.tvTitle.setText(postsBean.getTitle());
        holder.tv_number.setText(postsBean.getLikes_count() + "");
        Picasso.with(mContext)
                .load(postsBean.getCover_image_url())
                .into(holder.iv);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onItemClickListener(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public void seList(List<TitleBean.DataBean.PostsBean> posts) {
        this.list1 = posts;
        notifyDataSetChanged();
    }

    public void setList(List<AuthorBean.DataBean.PostsBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public class Holder extends RecyclerView.ViewHolder {
        private ImageView iv;
        private TextView tvTitle, tvTime, tv_number, tvNickName;

        public Holder(View v) {
            super(v);
            iv = (ImageView) v.findViewById(R.id.iv_item_author);
            tvTitle = (TextView) v.findViewById(R.id.tv_author_title);
            tvTime = (TextView) v.findViewById(R.id.tv_time);
            tv_number = (TextView) v.findViewById(R.id.tv_author_count);
            tvNickName = (TextView) v.findViewById(R.id.tv_nickname);
        }
    }

    private OnItemClickListener listener;

    public void setOnAuthorItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

}
